
//$debug.setMemoryLeakDetectionEnabled(false);//关闭内存泄露检测
if (!images.requestScreenCapture()) {
    console.log("申请截图权限不成功!!");
    exit();
} else {
    console.log("申请截图权限成功~~");
}
var intX = 0,intY = 0;
var 计数器 = 0;
var 延时 = 3000;
var 图片路径 =String(engines.myEngine().getSource()).replace("看广告.js","");
device.wakeUp();
// var 图片路径 = "/storage/emulated/0/DCIM/Screenshots/";

var 计数器重启 = Math.floor(Math.random()*(15-8)+8);
sleep(10000);
if(找图("1刷.png",true)){
    msg("1刷.png");}else{
        msg("11111刷.png");  
    }
//看广告();



function 看广告(){
click(1000, 250);
toastLog(计数器重启);
if(计数器>计数器重启){
    //重启
    if(!重启()){
        重启();
    }
    计数器=0;
}else{
    计数器++;
}

    msg(图片路径);
    toastLog('开始');
    toastLog("开始找图");

    //检查是否拉成功
    sleep(3000);
    for(let i=0;i<5;i++){
        click(1000, 250);
    }

    

//刷新
    if(找图("免费领取卡.png",true)){
            msg("免费领取卡.png");
            //判断是否不到4个
            if(找图("免费获取卡.png",true)){
                sleep(60*1000);
                back()
                msg("退出.png");
            }else{
                click(963, 670);
                sleep(1000);
                click(963, 670);
                sleep(1000);
                msg("不刷新.png");
            }
    }else{
                click(976, 345);
                sleep(1000);
                click(976, 345);
                sleep(1000);
                msg("没有免费领取.png");
        }
            
       

    
    msg("采集结束");
    return 0;
    
}

function 每级拉珠(拉珠数量,上兽数量){
    拉珠数量.forEach(point => {
        

        //防止弹广告
        click(1000, 270);
        sleep(1000);

        //防止弹采集成功
        click(1000, 270);

        click(point.x, point.y)
        sleep(1*1000);
        toastLog("开始找采集");
        
        if(找图偏移点击("采集.png",true,30,20)){
            msg("采集");
            上兽拉珠(上兽数量);
        }

        //点错采集 有调整内容
        if(找图("调整阵容.png",false)){
            click(1000, 270);
            msg("调整阵容.png");
        }

    });
  }


function 点击(intX,intY,sleepTime){
  click(intX,intY);
  sleep(sleepTime);
}

function msg(msgValue){
    toastLog(msgValue);
    console.log(msgValue);
}


function 找图(imgPath,isClick){
    let res = false;
    let myPath = 图片路径;
    //let myPath = "/storage/emulated/0/DCIM/Screenshots/";
    let wx = images.read(myPath + "res/" + imgPath);
    try{
        // 截图并找图
        let img = images.captureScreen();//截屏
        let p = findImage(img, wx);
        if(p){
            intX = p.x;
            intY = p.y;
            if(isClick){
                点击(intX,intY,延时);
            }
            res = true;
        }
        wx.recycle();//释放内存
        img.recycle();//释放内存
    }
    catch(err){
        //console.error(err);
    }
    
    return res;
}

function 找图偏移点击(imgPath,isClick,yidongX,yidongY){
    let res = false;
    let myPath = 图片路径;
    //let myPath = "/storage/emulated/0/DCIM/Screenshots/";
    let wx = images.read(myPath + "res/" + imgPath);
    try{
        // 截图并找图
        let img = images.captureScreen();//截屏
        let p = findImage(img, wx);
        if(p){
            intX = p.x+yidongX;
            intY = p.y+yidongY;
            if(isClick){
                点击(intX,intY,延时);
            }
            res = true;
        }
        wx.recycle();//释放内存
        img.recycle();//释放内存
    }
    catch(err){
        //console.error(err);
    }
    return res;
}


function 一图多找(imgPath){
        let myPath = 图片路径;
        let points;
        //let myPath = "/storage/emulated/0/DCIM/Screenshots/";
        let wx = images.read(myPath + "res/" + imgPath);
        try{
            // 截图并找图
            var img = captureScreen();
              points = images.matchTemplate(img, wx, {
                threshold: 0.9,
                max:10
            }).points;
    
            img.recycle();//释放内存
        }
        catch(err){
            //console.error(err);
        }

        wx.recycle();//释放内存
        img.recycle();//释放内存
        
        return points;
    }


function 上兽拉珠(上兽数量){
    sleep(2*1000);
    if(上兽数量=1){
        sleep(1*1000);
        click(344,1548);
        sleep(1*1000);
    }
    
click(732,1960);
sleep(1*1000);
click(245,1955);
sleep(1*1000);
}

function 多点找色(多点信息){
    let res = false;
    let img = images.captureScreen();//截屏
    sleep(200);//延迟一下，截图太快会卡顿
    let p = images.findMultiColors(img,多点信息[1],多点信息[2],多点信息[3]);
    if(p){
        res = true;
    }
    img.recycle();//释放内存
    return res;
}

function 重启(){
    let res = false;
    home();
    sleep(1*1000);
    click(240,2218);
    sleep(1*1000);
    gesture(1000,[550,1560],[550,460]);
    sleep(1*1000);
    click(832,2218);
    sleep(1*1000);
    if(找图("梦幻app.png",true)){
        sleep(50*1000);
        toastLog("进入梦幻");
        click(1000,300);
        if(找图("关闭公告.png",true)){
            toastLog("关闭公告");
        }
        sleep(1*1000);
        click(1000,300);
        sleep(1*1000);
        if(找图("进入战场.png",true)){
            toastLog("进入战场");
            res = true;
        }

    }
  }